package com.android.lovingpet.adopt.host;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.android.lovingpet.adapter.MyArrayAdapter;
import com.android.lovingpet.bean.Host;
import com.android.lovingpet.bean.JsonIds;
import com.android.lovingpet.bean.Pet;
import com.android.lovingpet.common.Back;
import com.android.lovingpet.util.CallbackStore;
import com.android.lovingpet.util.HttpUtil;
import com.android.lovingpet.util.LocationClient;
import com.android.lovingpet.util.MyHttp;
import com.android.lovingpet.util.URLStore;
import com.android.lovingpet.util.Utility;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by ikata on 2018/4/15.
 */

public class HostModel {

    public void getHostInfo(HashMap<String,String> params,Back back)
    {
        MyHttp.getInstance().postAsync(Utility.getJSessionId(), URLStore.HOST_GETINFO, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.onFailure(MyHttp.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responseData=response.body().string();
                int code=MyHttp.getCode(responseData);
                if (code==0)
                {
                    try {
                        String hostString=new JSONObject(responseData).getString("host");
                        Host host =new Gson().fromJson(hostString,Host.class);
                        Bundle bundle=new Bundle();
                        bundle.putParcelable("result", host);
                        back.onSuccess(bundle);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
                else
                {
                    back.onFailure(MyHttp.getCodeString(code));
                }
            }
        });

    }

    public void beHost(HashMap params, Back back)
    {
        HttpUtil.postAsync(Utility.getJSessionId(), URLStore.HOST_BEHOST, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responseData=response.body().string();
                Log.d("beHost",responseData);
                int code= MyHttp.getCode(responseData);
                if (code==0)
                {
                    back.onSuccess(null);
                }
            }
        });
    }
    public void sort(HashMap<String,String> params,Back back)
    {
        MyHttp.getInstance().postAsync(Utility.getJSessionId(), URLStore.HOST_SORT, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.onFailure(MyHttp.getFailureMeg());
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responseData=response.body().string();
                int code=MyHttp.getCode(responseData);
                if (code==0)
                {
                    JsonIds jsonIds=new Gson().fromJson(responseData,JsonIds.class);
                    Bundle bundle1=new Bundle();
                    bundle1.putIntegerArrayList("ids",jsonIds.getIds());
                    back.onSuccess(bundle1);
                }
                else
                {
                    back.onFailure(MyHttp.getCodeString(code));
                }
            }
        });
    }

    public void sortByTime(HashMap<String,String> params,Back back)
    {
        MyHttp.getInstance().postAsync(Utility.getJSessionId(),URLStore.HOST_SORT_BY_TIME,params,new CallbackStore.CallbackIds<Back>(back));
    }

}
